﻿using System;
using System.Windows;
using Halfwit.ViewModels;
using Halfwit.Views;

namespace Halfwit
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow(MainViewModel viewModel)
        {
            InitializeComponent();

            _notifyIcon = new System.Windows.Forms.NotifyIcon();
            _notifyIcon.Text = "Halfwit";
            
            var iconStream = Application.GetResourceStream(new Uri("pack://application:,,,/halfwit.ico")).Stream;
            _notifyIcon.Icon = new System.Drawing.Icon(iconStream);

            _notifyIcon.Click += _notifyIcon_Click;
            _notifyIcon.BalloonTipClicked += _notifyIcon_Click;
            _notifyIcon.Tag = WindowState.Normal;

            this.Top = Properties.Settings.Default.WindowTop;
            this.Left = Properties.Settings.Default.WindowLeft;
            this.Width = Properties.Settings.Default.WindowWidth;
            this.Height = Properties.Settings.Default.WindowHeight;

            mainFrame.Navigated += mainFrame_Navigated;

            _model = viewModel;
            _model.NavigationService = mainFrame.NavigationService;
            _model.Notify = Notify;
            this.DataContext = _model;

            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.Loaded -= MainWindow_Loaded;


            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Input, new Action(() => _model.Connect()));
            //_model.Connect();
        }

        MainViewModel _model;

        #region Notify Icon Helpers

        System.Windows.Forms.NotifyIcon _notifyIcon;
        bool _notifyMessageShown = false;

        void Notify(string text)
        {
            if (_notifyIcon.Visible && !String.IsNullOrEmpty(text))
            {
                _notifyIcon.ShowBalloonTip(5000, "Halfwit", text, System.Windows.Forms.ToolTipIcon.Info);
            }
        }

        void _notifyIcon_Click(object sender, EventArgs e)
        {
            WindowState = (WindowState)_notifyIcon.Tag;
        }

        protected override void OnStateChanged(EventArgs e)
        {
            base.OnStateChanged(e);

            if (!Properties.Settings.Default.MinimizeToTray) return;

            if (WindowState == WindowState.Minimized)
            {
                ShowInTaskbar = false;
                _notifyIcon.Visible = true;

                if (!_notifyMessageShown)
                {
                    _notifyIcon.ShowBalloonTip(1000, "Halfwit", "Halfwit is still running! You'll be notified of any new tweets. Click the notification or the Halfwit icon to restore.", System.Windows.Forms.ToolTipIcon.Info);
                    _notifyMessageShown = true;
                }
            }
            else
            {
                ShowInTaskbar = true;
                _notifyIcon.Tag = WindowState;
                _notifyIcon.Visible = !this.IsVisible;
            }
        }

        #endregion

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);
            
            if (TwitterConnection.User != null 
                && MessageBox.Show("Close Halfwit?", "Confirm Close", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.No)
            {
                e.Cancel = true;
                return;
            }

            Properties.Settings.Default.WindowTop = RestoreBounds.Top;
            Properties.Settings.Default.WindowLeft = RestoreBounds.Left;
            Properties.Settings.Default.WindowWidth = RestoreBounds.Width;
            Properties.Settings.Default.WindowHeight = RestoreBounds.Height;
            Properties.Settings.Default.Save();

            _notifyIcon.Dispose();
            _notifyIcon = null;
        }

        private void mainFrame_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            if (!(e.Content is TweetsPage)) return;

            mainFrame.NavigationService.RemoveBackEntry();

            GC.Collect();
        }
    }
}
